package org.onvivals.module.ums.service.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.undertow.util.BadRequestException;
import org.onvivals.common.enums.BillDetailEnum;
import org.onvivals.common.enums.OrderInfoEnum;
import org.onvivals.common.enums.PayTypeEnum;
import org.onvivals.common.exception.BusinessException;
import org.onvivals.module.ums.entity.UmsUser;
import org.onvivals.module.ums.entity.UmsUserRecharge;
import org.onvivals.module.ums.entity.UmsUserRole;
import org.onvivals.module.ums.mapper.UmsUserRechargeMapper;
import org.onvivals.module.ums.service.IUmsUserBillService;
import org.onvivals.module.ums.service.IUmsUserRechargeService;
import org.onvivals.module.ums.service.IUmsUserRoleService;
import org.onvivals.module.ums.service.IUmsUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;

@Service
public class UmsUserRechargeServiceImpl extends ServiceImpl<UmsUserRechargeMapper, UmsUserRecharge> implements IUmsUserRechargeService {

	@Autowired
	private IUmsUserService pmsMemberService;

	@Autowired
	private IUmsUserBillService umsUserBillService;

	@Autowired
	private IUmsUserRoleService userRoleService;

	@Override
	public String addVipRecharge(UmsUser pmsMember, String price, String name, String payType, int time) {
		if (StrUtil.isBlank(price) || StrUtil.isBlank(name)) {
			throw new BusinessException("参数非法");
		}
		UmsUserRecharge yxUserRecharge = new UmsUserRecharge();

		String orderSn = IdUtil.getSnowflake(0, 0).nextIdStr();

		yxUserRecharge.setNickname(pmsMember.getNickname());
		yxUserRecharge.setOrderId(orderSn);
		yxUserRecharge.setUid(pmsMember.getUid());
		yxUserRecharge.setPrice(new BigDecimal(price));
		yxUserRecharge.setRechargeType(PayTypeEnum.WEIXIN.getValue());
		yxUserRecharge.setRechargeMethod(payType);
		yxUserRecharge.setPaid(OrderInfoEnum.PAY_STATUS_0.getValue());
		yxUserRecharge.setTime(time);
		yxUserRecharge.setName(name);

		baseMapper.insert(yxUserRecharge);

		return orderSn;
	}

	@Override
	public String addRoleRecharge(UmsUser pmsMember, String price, String name, String payType,Long busId) {
		if (StrUtil.isBlank(price) || StrUtil.isBlank(name)) {
			throw new BusinessException("参数非法");
		}
		UmsUserRecharge yxUserRecharge = new UmsUserRecharge();

		String orderSn = IdUtil.getSnowflake(0, 0).nextIdStr();

		yxUserRecharge.setNickname(pmsMember.getNickname());
		yxUserRecharge.setOrderId(orderSn);
		yxUserRecharge.setUid(pmsMember.getUid());
		yxUserRecharge.setPrice(new BigDecimal(price));
		yxUserRecharge.setRechargeType(PayTypeEnum.WEIXIN.getValue());
		yxUserRecharge.setRechargeMethod(payType);
		yxUserRecharge.setPaid(OrderInfoEnum.PAY_STATUS_0.getValue());
		yxUserRecharge.setBusId(busId);
		yxUserRecharge.setName(name);

		baseMapper.insert(yxUserRecharge);

		return orderSn;
	}

	@Override
	public String addRecharge(UmsUser pmsMember, String price, String giveIntegral, String payType) {
		if (StrUtil.isBlank(price) || StrUtil.isBlank(giveIntegral)) {
			throw new BusinessException("参数非法");
		}
		UmsUserRecharge yxUserRecharge = new UmsUserRecharge();

		String orderSn = IdUtil.getSnowflake(0, 0).nextIdStr();

		yxUserRecharge.setNickname(pmsMember.getNickname());
		yxUserRecharge.setOrderId(orderSn);
		yxUserRecharge.setUid(pmsMember.getUid());
		yxUserRecharge.setPrice(new BigDecimal(price));
		yxUserRecharge.setGiveRole(Convert.toInt(giveIntegral));
		yxUserRecharge.setRechargeType(PayTypeEnum.WEIXIN.getValue());
		yxUserRecharge.setRechargeMethod(payType);
		yxUserRecharge.setPaid(OrderInfoEnum.PAY_STATUS_0.getValue());

		baseMapper.insert(yxUserRecharge);

		return orderSn;
	}

	@Override
	public UmsUserRecharge getInfoByOrderId(String orderId) {
		UmsUserRecharge memberRecharge = this.getOne(Wrappers.<UmsUserRecharge>lambdaQuery().eq(UmsUserRecharge::getOrderId, orderId));
		return memberRecharge;
	}

	@Override
	public void updateRecharge(UmsUserRecharge userRecharge) {
		UmsUser user = pmsMemberService.getById(userRecharge.getUid());
		//修改状态
		userRecharge.setPaid(OrderInfoEnum.PAY_STATUS_1.getValue());
		userRecharge.setPayTime(new Date());
		this.updateById(userRecharge);
		if ("integral".equals(userRecharge.getRechargeMethod())) {
			//修改状态
			userRecharge.setPaid(OrderInfoEnum.PAY_STATUS_1.getValue());
			userRecharge.setPayTime(new Date());
			this.updateById(userRecharge);
			//最终充值金额
			BigDecimal newPrice = NumberUtil.add(userRecharge.getGiveRole(), user.getRoleNum());
			//update 余额
			user.setRoleNum(newPrice.intValue());
		}
		if ("vip".equals(userRecharge.getRechargeMethod())) {
			//获取当前用户vip时间
			Date vipExpireTime = user.getVipExpireTime();
			if (vipExpireTime != null) {
				//更新用户vip时间
				Date newVipExpireTime = DateUtil.offsetMonth(vipExpireTime, userRecharge.getTime());
				user.setVipExpireTime(newVipExpireTime);
			} else {
				//更新用户vip时间
				Date newVipExpireTime = DateUtil.offsetMonth(new Date(), userRecharge.getTime());
				user.setVipExpireTime(newVipExpireTime);
			}
			//增加流水
			umsUserBillService.income(userRecharge.getUid(), userRecharge.getName(), BillDetailEnum.CATEGORY_2.getValue(),
					BillDetailEnum.TYPE_13.getValue(),userRecharge.getPrice().doubleValue(), 0,
					"开通会员", userRecharge.getId().toString(),user.getVipExpireTime());
			pmsMemberService.updateById(user);
		}if ("role".equals(userRecharge.getRechargeMethod())) {
			//增加当前的用户角色
			UmsUserRole umsUserRole = new UmsUserRole();
			umsUserRole.setUserId(userRecharge.getUid());
			umsUserRole.setRoleId(userRecharge.getBusId());
			userRoleService.save(umsUserRole);

			//增加流水
			umsUserBillService.income(userRecharge.getUid(), userRecharge.getName(), BillDetailEnum.CATEGORY_2.getValue(),
					BillDetailEnum.TYPE_14.getValue(),userRecharge.getPrice().doubleValue(), 0,
					"购买角色", userRecharge.getId().toString(),user.getVipExpireTime());
			pmsMemberService.updateById(user);
		}
		pmsMemberService.updateById(user);
	}
}
